<?php

namespace App\Http\Resources\OpenShop\ShopLocation;

use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignCostMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignSoftOutfitMappingModel;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class SiteMissionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "id"             => $this->id,
            "mainStep"       => $this->mainStep,
            "mainStepText"   => MainProcessEnum::MAIN_PROCESS_MAP[$this->mainStep],
            "stepStatus"     => $this->stepStatus,
            "stepStatusText" => MissionTemplateEnum::STEP_STATUS_TEXT[$this->stepStatus],
            "missionName"    => $this->missionName,
            "status"         => $this->status,
            "statusText"     => SiteMissionEnum::STATUS_TEXT[$this->status],
            "orderNo"        => $this->orderNo,
            "dealUid"        => $this->dealUid,
            "handlerType"    => $this->handlerType,
            "expiredAt"      => formatDate($this->expiredAt),
            "completeAt"     => formatDate($this->completeAt),
            "isExpired"      => !($this->expiredAt > time()),
            "createdAt"      => formatDate($this->createdAt->timestamp),
            "prepareInfo"    => $this->getPrepareInfo()
        ];
    }

    public function getPrepareInfo()
    {
        $prepareInfo = [];
        switch ($this->stepStatus) {
            case MissionTemplateEnum::STEP_STATUS_14:
                $prepareInfo = OpenShopSpaceDesignCostMappingModel::query()
                                                                  ->where("orderNo", $this->orderNo)
                                                                  ->where("orderStatus",
                                                                      OpenShopSpaceDesignCostMappingModel::ORDER_STATUS_0)
                                                                  ->select([
                                                                      "id",
                                                                      "orderNo",
                                                                      "preparationNo",
                                                                      "orderStatus",
                                                                      "payStatus"
                                                                  ])
                                                                  ->get()->toArray();
                break;
            case MissionTemplateEnum::STEP_STATUS_17:
                $prepareInfo = OpenShopSpaceDesignSoftOutfitMappingModel::query()
                                                                        ->where("orderNo", $this->orderNo)
                                                                        ->where("orderStatus",
                                                                            OpenShopSpaceDesignCostMappingModel::ORDER_STATUS_0)
                                                                        ->select([
                                                                            "id",
                                                                            "orderNo",
                                                                            "preparationNo",
                                                                            "orderStatus",
                                                                            "payStatus"
                                                                        ])
                                                                        ->get()->toArray();
                break;
        }
        return $prepareInfo;
    }
}
